﻿using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using NPOI.SS.UserModel;
using Warehouse.API.Applcation.Command.EnterStoragesCommand;
using Warehouse.Domain;
using Warehouse.Domain.DTO;
using Warehouse.Infrstructre;
using Warehouse.Infrstructre.Interface;
using static WMSpublic.ParseAPI;
using WMSpublic;

namespace Warehouse.API.Applcation.CommandHandlers.EnterStoragesCommandHandler
{
    public class EnterStoragesCreateCommandHandler : IRequestHandler<EnterStoragesCreateCommand, int>
    {
        protected readonly IEnterStoragesRepsitory enterStorages;
        protected readonly IEnterStoragesInfoRepsitory infoRepsitory;
        protected readonly EFCoreDBcontext db;
        protected readonly IMapper mapper;

        public EnterStoragesCreateCommandHandler(IEnterStoragesRepsitory enterStorages, IEnterStoragesInfoRepsitory infoRepsitory, EFCoreDBcontext db, IMapper mapper)
        {
            this.enterStorages = enterStorages;
            this.infoRepsitory = infoRepsitory;
            this.db = db;
            this.mapper = mapper;
        }

        public async Task<int> Handle(EnterStoragesCreateCommand request, CancellationToken cancellationToken)
        {
            using (var tran = db.Database.BeginTransaction(System.Data.IsolationLevel.Serializable))
            {
                try
                {
                    var s = mapper.Map<StockRoom>(request.dto);
                    await enterStorages.AddAsync(s);
                    foreach (var item in request.dto.StockProducts)
                    {
                        var pro = new StockProducts()
                        {
                            StockId = s.StockId,
                            ProductsID = item.ProductsID,
                            Quantity = item.Quantity,
                            Batch = item.Batch,
                            Sprice = item.Sprice,
                            Binlocation = item.Binlocation,
                            office_id = s.office_id,
                            user_id = item.user_id,
                            AddName = s.AddName,
                            AddTime = DateTime.Now,
                            StateSP = 1
                        };
                        await infoRepsitory.AddAsync(pro);
                    }
                    MessageCreateDto a = new MessageCreateDto();
                    a.AddName = "张三";
                    a.AddTime = DateTime.Now;
                    a.Description = "入库新增";
                    a.Title = "入库新增";
                    a.CurrenTime = DateTime.Now;
                    a.MessageType = "添加";
                    a.MessageState = false;
                    a.Office_Id = "1";
                    await MessageAdds(a);
                    tran.Commit();
                    return 1;
                }
                catch (Exception)
                {
                    tran.Rollback();
                    throw;
                }
            }
        }

        public async Task<int> MessageAdds(MessageCreateDto a)
        {
            string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIyNCIsInNpZCI6IjE4NTk4MDM1MDY2MzA4NTY3MDQiLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL2dyb3Vwc2lkIjoiMSIsImp0aSI6IjQzMzA2NmRmLWYyNjAtNDI2Ny1iYWY5LWJkMzgxZGVhZGMwYiIsImh0dHA6Ly9zY2hlbWFzLnhtbHNvYXAub3JnL3dzLzIwMDUvMDUvaWRlbnRpdHkvY2xhaW1zL25hbWUiOiLmnLHms4rpnJYiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy91cmkiOiIiLCJpc3MiOiJSZW5hb2JvMjAwNCIsImF1ZCI6IldlaWZ1d3UyMjAzQSJ9.E0PG5CbU-_2efHW14aTC5dmRc3FC7BLU0TIgjNsA1wo";
            HttpClientWrapper client = new HttpClientWrapper(token);
            string url = "http://116.205.121.197:8000/xxx/api/MessageCreate/CreateMessage";
            //将a转换为json字符串
            string jsonStr = JsonConvert.SerializeObject(a);
            string postRep = await client.PostAsync(url, jsonStr);
            return 1;
        }

    }
}
